package wty;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ChangeAdministratorPassword {

    public static JPanel panel;

    public ChangeAdministratorPassword() {
        init();
    }

    private void init() {
        panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBackground(Color.lightGray);

        JLabel title = new JLabel("Please enter your new password:");
        title.setFont(new Font("", Font.PLAIN, 24));
        title.setForeground(Color.BLUE);
        title.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(title, BorderLayout.NORTH);

        // 设置密码输入框，调整字体、大小、边框
        JTextField passwordField = new JTextField();
        passwordField.setFont(new Font("", Font.PLAIN, 58)); // 字体大小、样式
        passwordField.setPreferredSize(new Dimension(250, 100)); // 更改输入框的尺寸
        passwordField.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1)); // 设置边框
        passwordField.setHorizontalAlignment(SwingConstants.CENTER); // 内容居中
        panel.add(passwordField, BorderLayout.CENTER);

        // 设置按钮
        JButton changeButton = new JButton("Change Password");
        changeButton.setFont(new Font("", Font.PLAIN, 20));
        changeButton.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String newPassword = passwordField.getText();

                        if (!newPassword.isEmpty()) {
                            //更改成功
                            Login.setAdministrator_Password(newPassword);
                            // 弹出对话框
                            JOptionPane.showMessageDialog(ChangeAdministratorPassword.panel, "Password changed successfully!");


                            //返回
                            ChangeAdministratorPassword.panel.setVisible(false);
                            Administrator.panel.setVisible(true);
                        } else {
                            JOptionPane.showMessageDialog(ChangeAdministratorPassword.panel, "Password cannot be empty.", "ERROR!", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
        );

        JButton backButton = new JButton("Back");
        backButton.setFont(new Font("", Font.PLAIN, 20));
        backButton.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        ChangeAdministratorPassword.panel.setVisible(false);
                        Administrator.panel.setVisible(true);
                    }
                }
        );

        // 按钮面板
        JPanel ButtonPanel = new JPanel();
        ButtonPanel.add(changeButton);
        ButtonPanel.add(backButton);
        panel.add(ButtonPanel, BorderLayout.SOUTH);
    }
}
